<template>
    <div>
        <div class="operarea">
            <div class="seach">
                <el-input v-model.trim="seachForm.userid" :placeholder="$t('username')" clearable size="mini"></el-input>
                <el-button size="mini" icon="el-icon-search" @click="getData"></el-button>
            </div>
            <div class="btnList">
                <el-button size="mini" @click="addDialogVisible = true">{{$t('add')}}</el-button>
            </div>
        </div>
        <el-card class="cardBox">
            <el-breadcrumb separator="/">
                <el-breadcrumb-item>首页</el-breadcrumb-item>
                <el-breadcrumb-item>后台用户</el-breadcrumb-item>
            </el-breadcrumb>
            <el-table style="width: 100%" :data="sysUserTab" stripe v-adaptive="{bottomOffset: 85}" height="100">
                <el-table-column prop="userid" :label="$t('username')" min-width="180">
                </el-table-column>
                <!-- <el-table-column prop="phone" :label="$t('phonenum')" min-width="180">
                </el-table-column> -->
                <el-table-column prop="roleName" :label="$t('userrole')" min-width="180">
                    <template v-slot="scope">
                        <el-button @click="showModifyRole(scope.row.id)" size="mini">{{$t('modifytherole')}}</el-button>
                    </template>
                </el-table-column>
                <el-table-column prop="status" :label="$t('status')" min-width="100">
                    <template v-slot="scope">
                        <el-switch v-model="scope.row.status" active-color="#13ce66" inactive-color="#ff4949" :active-value="0" :inactive-value="1" @change="handleStatus(scope.row)"></el-switch>
                    </template>
                </el-table-column>
                <el-table-column prop="address" :label="$t('operation')" width="300">
                    <template v-slot="scope">
                        <el-button type="primary" @click="showModify(scope.row.id)" size="mini">编辑</el-button>
                        <el-button type="primary" @click="showModifyPwd(scope.row.id)" size="mini">{{$t('changepwd')}}</el-button>
                        <el-button type="primary" @click="showClose(scope.row.id)" size="mini">{{$t('close')}}</el-button>
                    </template>
                </el-table-column>
            </el-table>
            <el-pagination
                small
                @size-change="handleSizeChange"
                @current-change="handleCurrentChange"
                :pager-count="11"
                :current-page.sync="seachForm.pageNo"
                :page-sizes="[10, 20, 30, 40]"
                :page-size="seachForm.pageSize"
                layout="sizes, prev, pager, next"
                :total="total">
            </el-pagination>
        </el-card>
        <!-- 添加 -->
        <el-dialog :title="$t('add')" :visible.sync="addDialogVisible" width="500px" class="smallDlog" :close-on-click-modal="false" @close="addDlogClose(addDataForm)">
            <el-form :model="addDataForm" :rules="addRules" :ref="addDataForm" class="motuoForm">
                <el-form-item :label="$t('username')" label-width="70px" prop="userid">
                    <el-input v-model="addDataForm.userid" clearable :placeholder="$t('plsent')"></el-input>
                </el-form-item>
                <el-form-item :label="$t('password')" label-width="70px" prop="password">
                    <el-input v-model="addDataForm.password" type="password" clearable :placeholder="$t('plsent')"></el-input>
                </el-form-item>
                <!-- <el-form-item :label="$t('phone')" label-width="70px" prop="phone">
                    <el-input v-model="addDataForm.phone" clearable :placeholder="$t('plsent')"></el-input>
                </el-form-item>
                <el-form-item :label="$t('email')" label-width="70px" prop="email">
                    <el-input v-model="addDataForm.email" clearable :placeholder="$t('plsent')"></el-input>
                </el-form-item> -->
                <el-form-item :label="$t('remark')" label-width="70px" prop="remarks">
                    <el-input v-model="addDataForm.remarks" clearable :placeholder="$t('plsent')"></el-input>
                </el-form-item>
                <el-form-item :label="$t('status')" label-width="70px" prop="status">
                    <el-radio-group v-model="addDataForm.status">
                        <el-radio :label="1">{{$t('forbidden')}}</el-radio>
                        <el-radio :label="0">{{$t('invoke')}}</el-radio>
                    </el-radio-group>
                </el-form-item>
            </el-form>
            <span slot="footer" class="dialog-footer">
                <el-button @click="addDialogVisible = false">{{$t('cancel')}}</el-button>
                <el-button type="primary" @click="addData(addDataForm)">{{$t('confirm')}}</el-button>
            </span>
        </el-dialog>
        <!-- 修改 -->
        <el-dialog :title="$t('modify')" :visible.sync="modifyDialogVisible" width="500px" class="smallDlog" :close-on-click-modal="false" @close="modifyDlogClose(modifyDataForm)">
            <el-form :model="modifyDataForm" :rules="modifyRules" :ref="modifyDataForm" class="motuoForm">
                <el-form-item :label="$t('username')" label-width="70px" prop="userid">
                    <el-input v-model="modifyDataForm.userid" clearable :placeholder="$t('plsent')"></el-input>
                </el-form-item>
                <!-- <el-form-item :label="$t('password')" label-width="70px" prop="password">
                    <el-input v-model="modifyDataForm.password" type="password" show-password clearable :placeholder="$t('plsent')"></el-input>
                </el-form-item> -->
                <!-- <el-form-item :label="$t('phone')" label-width="70px" prop="phone">
                    <el-input v-model="modifyDataForm.phone" clearable :placeholder="$t('plsent')"></el-input>
                </el-form-item>
                <el-form-item :label="$t('email')" label-width="70px" prop="email">
                    <el-input v-model="modifyDataForm.email" clearable :placeholder="$t('plsent')"></el-input>
                </el-form-item> -->
                <el-form-item :label="$t('remark')" label-width="70px" prop="remarks">
                    <el-input v-model="modifyDataForm.remarks" clearable :placeholder="$t('plsent')"></el-input>
                </el-form-item>
            </el-form>
            <span slot="footer" class="dialog-footer">
                <el-button @click="modifyDialogVisible = false">{{$t('cancel')}}</el-button>
                <el-button type="primary" @click="modifyData(modifyDataForm)">{{$t('confirm')}}</el-button>
            </span>
        </el-dialog>
        <!-- 删除 -->
        <el-dialog :title="$t('hint')" :visible.sync="delDialogVisible" width="420px" class="smallDlog" :close-on-click-modal="false">
            <span>{{$t('confirmclo')}}</span>
            <span slot="footer" class="dialog-footer">
                <el-button @click="delDialogVisible = false">{{$t('cancel')}}</el-button>
                <el-button type="primary" @click="delData">{{$t('confirm')}}</el-button>
            </span>
        </el-dialog>
        <!-- 修改密码 -->
        <el-dialog :title="$t('modify')" :visible.sync="modifyPwdDialogVisible" width="500px" class="smallDlog" :close-on-click-modal="false" @close="modifyPwdDlogClose(modifyPwdForm)">
            <el-form :model="modifyPwdForm" :rules="modifyPwdRules" :ref="modifyPwdForm" class="motuoForm">
                <el-form-item :label="$t('password')" label-width="70px" prop="password">
                    <el-input v-model="modifyPwdForm.password" type="password" show-password clearable :placeholder="$t('plsent')" @keyup.enter.native="modifyPwdData(modifyPwdForm)"></el-input>
                </el-form-item>
            </el-form>
            <span slot="footer" class="dialog-footer">
                <el-button @click="modifyPwdDialogVisible = false">{{$t('cancel')}}</el-button>
                <el-button type="primary" @click="modifyPwdData(modifyPwdForm)">{{$t('confirm')}}</el-button>
            </span>
        </el-dialog>
        <!-- 修改用户角色 -->
        <el-dialog :title="$t('modify')" :visible.sync="modifyRoleDialogVisible" width="500px" class="smallDlog" :close-on-click-modal="false" @close="modifyRoleDlogClose(modifyRoleForm)">
            <el-form :model="modifyRoleForm" :rules="modifyRoleRules" :ref="modifyRoleForm" class="motuoForm">
                <el-form-item :label="$t('rolename')" label-width="70px" prop="list">
                    <el-select v-model="modifyRoleForm.list" multiple :placeholder="$t('plssel')">
                        <el-option v-for="item in roleList" :key="item.id" :label="item.name" :value="item.id"></el-option>
                    </el-select>
                </el-form-item>
            </el-form>
            <span slot="footer" class="dialog-footer">
                <el-button @click="modifyRoleDialogVisible = false">{{$t('cancel')}}</el-button>
                <el-button type="primary" @click="modifyRoleData(modifyRoleForm)">{{$t('confirm')}}</el-button>
            </span>
        </el-dialog>
    </div>
</template>

<script>
import {
    getSysUserList, 
    addSysUser,
    modifySysUserStatus,
    removeSysUser,
    getSysUserDetails,
    getSysUserRoles,
    getAllRole,
    updateSysUserRole
} from '@/libs/reqlist.js'
export default {
    data () {
        // var isPhone = (rule, value, callback) => {
        //     var pattern = /^1([38][0-9]|4[579]|5[0-3,5-9]|6[6]|7[0135678]|9[89])\d{8}$/
        //     if (!pattern.test(value)) {
        //         return callback(new Error(this.$t('pleaseenterthecorrectcellphonenumber')))
        //     } else {
        //         callback()
        //     }
        // }
        var isPwd = (rule, value, callback) => {
            var pattern = /^(?![0-9]+$)(?![a-zA-Z]+$)[0-9A-Za-z]{6,12}$/
            if (!pattern.test(value)) {
                return callback(new Error(this.$t('pleaseenterthepasswordinthecorrectformat')))
            } else {
                callback()
            }
        }
        return {
            total: 0,
            seachForm: {
                userid: '',
                pageNo: 1,
                pageSize: 10
            },
            sysUserTab: [],
            addDialogVisible: false,
            addDataForm: {
                userid: '',
                password: '',
                phone: '',
                email: '',
                remarks: '',
                status: 0
            },
            addRules: {
                userid: [
                    { required: true, message: this.$t('cannotbeemp'), trigger: 'blur' }
                ],
                password: [
                    { required: true, message: this.$t('cannotbeemp'), trigger: 'blur' },
                    { validator: isPwd, trigger: 'blur' }
                ]
                // phone: [
                //     { required: true, message: this.$t('cannotbeemp'), trigger: 'blur' },
                //     { validator: isPhone, trigger: 'blur' }
                // ],
                // email: [
                //     { required: true, message: this.$t('cannotbeemp'), trigger: 'blur' },
                //     { type: 'email', message: this.$t('pleaseenterthecorrectemailaddress'), trigger: 'blur' }
                // ]
            },
            modifyDialogVisible: false,
            modifyDataForm: {
                id: 0,
                userid: '',
                password: '',
                phone: '',
                email: '',
                remarks: '',
                status: 0,
                createTime: '',
                updateTime: ''
            },
            modifyRules: {
                userName: [
                    { required: true, message: this.$t('cannotbeemp'), trigger: 'blur' }
                ]
                // phone: [
                //     { required: true, message: this.$t('cannotbeemp'), trigger: 'blur' },
                //     { validator: isPhone, trigger: 'blur' }
                // ],
                // email: [
                //     { required: true, message: this.$t('cannotbeemp'), trigger: 'blur' },
                //     { type: 'email', message: this.$t('pleaseenterthecorrectemailaddress'), trigger: 'blur' }
                // ]
            },
            modifyPwdDialogVisible: false,
            modifyPwdForm: {
                id: 0,
                password: ''
            },
            modifyPwdRules: {
                password: [
                    { required: true, message: this.$t('cannotbeemp'), trigger: 'blur' },
                    { validator: isPwd, trigger: 'blur' }
                ]
            },
            delUserId: '',
            delDialogVisible: false,
            modifyStatusForm: {
                id: '',
                status: 0
            },
            roleList: [],
            modifyRoleDialogVisible: false,
            modifyRoleForm: {
                id: '',
                list: []
            },
            modifyRoleRules: {
                list: [
                    { required: true, message: this.$t('cannotbeemp'), trigger: 'change' }
                ]
            }
        }
    },
    mounted () {
        this.seachData()
    },
    methods: {
        seachData () {
            getSysUserList(this.seachForm).then(res => {
                if (res.data.code === 0) {
                    this.total = res.data.data.hits
                    this.sysUserTab = res.data.data.data
                }
            })
        },
        getData () {
            this.seachForm.pageNo = 1
            this.seachData()
        },
        handleSizeChange (newSize) {
            this.seachForm.pageSize = newSize
            this.seachData()
        },
        handleCurrentChange (newNum) {
            this.seachForm.pageNo = newNum
            this.seachData()
        },
        addDlogClose (form) {
            this.$refs[form].resetFields()
        },
        addData (form) {
            this.$refs[form].validate(valid => {
                if (!valid) return
                addSysUser(JSON.stringify(this.addDataForm)).then(res => {
                    if (res.data.code === 0) {
                        this.$message({
                            type: 'success',
                            message: this.$t('sucadd')
                        })
                        this.addDialogVisible = false
                        this.seachData()
                    }
                })
                
            })
        },
        showModify (id) {
            getSysUserDetails(id).then(res => {
                if (res.data.code === 0) {
                    this.modifyDataForm = res.data.data
                    this.modifyDataForm.password = ''
                    delete this.modifyDataForm.createTime
                    delete this.modifyDataForm.updateTime
                }
            })
            this.modifyDialogVisible = true
        },
        modifyDlogClose (form) {
            this.$refs[form].resetFields()
        },
        modifyData (form) {
            this.$refs[form].validate(valid => {
                if (!valid) return
                addSysUser(JSON.stringify(this.modifyDataForm)).then(res => {
                    if (res.data.code === 0) {
                        this.$message({
                            type: 'success',
                            message: this.$t('modifysuc')
                        })
                        this.modifyDialogVisible = false
                        this.seachData()                    }
                })
            })
        },
        showModifyPwd (id) {
            this.modifyPwdForm.id = id
            this.modifyPwdDialogVisible = true
        },
        modifyPwdDlogClose (form) {
            this.$refs[form].resetFields()
        },
        modifyPwdData (form) {
            this.$refs[form].validate(valid => {
                if (!valid) return
                addSysUser(JSON.stringify(this.modifyPwdForm)).then(res => {
                    if (res.data.code === 0) {
                        this.$message({
                            type: 'success',
                            message: this.$t('modifysuc')
                        })
                        this.modifyPwdDialogVisible = false
                        this.seachData()
                    }
                })
            })
        },
        showClose (id) {
            this.delUserId = id
            this.delDialogVisible = true
        },
        delData () {
            removeSysUser(this.delUserId).then(res => {
                if (res.data.code === 0) {
                    this.$message({
                        type: 'success',
                        message: this.$t('canofsuc')
                    })
                    this.delDialogVisible = false
                    this.seachData()
                }
            })
        },
        handleStatus (item) {
            this.modifyStatusForm.id = item.id
            switch (item.status) {
                case 0:
                    item.status = 1
                    break
                case 1:
                    item.status = 0
                    break
            }
            this.$confirm(this.$t('whethertomodifytherolestate'), this.$t('hint'), {
                confirmButtonText: this.$t('confirm'),
                cancelButtonText: this.$t('cancel'),
                cutomClass: 'messageBox-lion'
            }).then(() => {
                this.modifyStatusForm.status = item.status
                modifySysUserStatus(this.modifyStatusForm).then(res => {
                    if (res.data.code === 0) {
                        this.$message({
                            type: 'success',
                            message: this.$t('statuschangesuccessful')
                        })
                        this.seachData()
                    }
                })
            }).catch(() => {})
        },
        async showModifyRole (id) {
            await getAllRole().then(res => {
                if (res.data.code === 0) {
                    this.roleList = res.data.data
                }
            })
            await getSysUserRoles(id).then(res => {
                if (res.data.code === 0) {
                    res.data.data.forEach(item => {
                        this.modifyRoleForm.list.push(item.id)
                    })
                    this.modifyRoleForm.id = id
                }
            })
            this.modifyRoleDialogVisible = true
        },
        modifyRoleData (form) {
            this.$refs[form].validate((valid) => {
                if (!valid) return
                updateSysUserRole(this.modifyRoleForm.id, this.modifyRoleForm.list).then(res => {
                    if (res.data.code === 0) {
                        this.$message({
                            type: 'success',
                            message: this.$t('modifysuc')
                        })
                        this.modifyRoleDialogVisible = false
                        this.seachData()
                    }
                })
            })
        },
        modifyRoleDlogClose (form) {
            this.$refs[form].resetFields()
            this.modifyRoleForm.list = []
        }
    }
}
</script>

<style lang="less" scoped>
@import './sysuser.less';
</style>
